<template>
  <div>
    <basic-container>
      <avue-crud :option="option"
                :table-loading="loading"
                :data="data"
                :page="page"
                :permission="permissionList"
                :before-open="beforeOpen"
                v-model="form"
                ref="crud"
                @row-update="rowUpdate"
                @row-save="rowSave"
                @row-del="rowDel"
                @search-change="searchChange"
                @search-reset="searchReset"
                @selection-change="selectionChange"
                @current-change="currentChange"
                @size-change="sizeChange"
                @refresh-change="refreshChange"
                @on-load="onLoad">
        <template slot-scope="scope" slot="menu">
          <el-button type="text"
                    size="small"
                    icon="el-icon-view"
                    plain
                    @click="handleView(scope.row.id)">查看
          </el-button>
        </template>
      </avue-crud>
    </basic-container>

    <!-- 人员详情 -->
    <el-dialog
      title="人员详情"
      ref="peopleDetailDialog"
      append-to-body
      :visible.sync="peopleDetailFlowBox"
      :fullscreen="false"
      custom-class="detail_dialog"
    >
      <people-detail style="height:750px;overflow-y:auto;" :peopleId="peopleId" :key="componentKey" :dictArr="dictItemArr" />
    </el-dialog>
  </div>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/ninebasic/realpop/allPopulation";
  import {mapGetters} from "vuex";
  import { getHouseList } from "@/api/ninebasic/realpop/house";
  import { validatenull,isvalidatemobile,cardid} from "@/util/validate";
  import peopleDetail from '@/components/gis/peopleDetail'

  export default {
    data() {
      //手机号校验非空
      const validatePhone = (rule, value, callback) => {
        if (isvalidatemobile(value)[0]) {
          callback(new Error(isvalidatemobile(value)[1]));
        } else {
          callback();
        }
      };
      //校验身份正号码
      const validateCardid = (rule, value, callback) => {
        if(cardid(value)[0]){
          callback(new Error(cardid(value)[1]))
        }else{
          callback();
        }
      };
      return {
        form: {},
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        peopleDetailFlowBox:false,
        peopleId:'',
        dictItemArr:{
          sex: [{value:'1',text:"男"},{value:'0',text:"女"}],
          nation:[],
          party:[],
          religion:[]
        },

        selectionList: [],
        option: {
           height: 'auto',
          calcHeight: 30,
          dialogWidth: 1250,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: false,
          addBtn: false,
          editBtn: false,
          delBtn: false,
          selection: true,
          excelBtn: true,
          menuWidth: 150,
          dialogClickModal: false,
          column: [
            // {
            //   label: "头像",
            //   prop: "photoLink",
            //   type: 'upload',
            //   listType: 'picture-img',
            //   action: '/api/smart-resource/oss/endpoint/put-file',
            //   propsHttp: {
            //     res: 'data',
            //     url: 'link',
            //   },
            //   hide: true,
            //   span: 24,
            // },
           {
              label: "所属网格",
              prop: "gridId",
              type: "tree",
              span: 24,
              parent: true,
              display: true,
              dicMethod: "get",
              dicUrl: "/api/smart-system/dept/treeByPid",
              props: {
                label: "title"

              },
              checkStrictly: true,
              search: true,
              rules: [{
                required: true,
                message: "请输入所属网格",
                trigger: "click"
              }],
            },
            {
              label: "房屋",
              prop: "houseId",
              type: "select",
              hide: true,
              span: 8,
               dicData: [],
               props: {
                    label: "title",
                    value: "key"
                  },
              rules: [{
                required: true,
                message: "请输入房屋",
                trigger: "blur"
              }],
            },
            {
              label: "身份证号码",
              prop: "idCardNo",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                validator: validateCardid,
                //message: "请输入身份号码",
                trigger: "blur"
              }],
              blur: ({value}) => {
                //this.$message.success(value,column)
                this.form.birthday  = value.substring(6,10)+"-"+value.substring(10,12)+"-"+value.substring(12,14);
                this.form.sex = value.substring(16,17)%2===0?'2':'1';
              }
            },
            {
              label: "姓名",
              prop: "name",
              type: "input",
              search: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入姓名",
                trigger: "blur"
              }],
            },
            {
              label: "曾用名",
              prop: "usedName",
              type: "input",
              span: 8,
              hide: true,
            },
            {
              label: "性别 ",
              prop: "sex",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=sex",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              search: true,
              rules: [{
                required: true,
                message: "请输入性别 ",
                trigger: "blur"
              }],
            },
            {
              label: "出生日期",
              prop: "birthday",
              type: "date",
              format: "yyyy-MM-dd",
              valueFormat: "yyyy-MM-dd",
              span: 8,
              search: true,
              rules: [{
                required: true,
                message: "请输入出生日期",
                trigger: "blur"
              }],
            },
            {
              label: "民族",
              prop: "nation",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=nation",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入民族",
                trigger: "blur"

              }],
            },
            {
              label: "籍贯",
              prop: "nativePlace",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入籍贯",
                trigger: "blur"
              }],
            },
            {
              label: "婚姻状况",
              prop: "marriage",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=marital_status",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入婚姻状况",
                trigger: "blur"
              }],
            },
            {
              label: "政治面貌",
              prop: "politics",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=party",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入政治面貌",
                trigger: "blur"
              }],
            },
            {
              label: "学历",
              prop: "education",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=education",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入学历",
                trigger: "blur"
              }],
            },
            {
              label: "宗教信仰",
              prop: "religion",
              type: "select",
              dicUrl: "/api/smart-system/dict/dictionary?code=religion",
              span: 8,
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              hide: true,
              rules: [{
                required: true,
                message: "请输入宗教信仰",
                trigger: "blur"
              }],
            },
            {
              label: "职业类别",
              prop: "professionType",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入职业类别",
                trigger: "blur"
              }],
            },
            {
              label: "职业",
              prop: "profession",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入职业",
                trigger: "blur"
              }],
            },
            {
              label: "工作单位",
              prop: "serviceSpace",
              type: "input",
              span: 8,
              hide: true,
            },
            {
              label: "联系方式",
              prop: "mobile",
              type: "input",
              search: true,
              span: 8,
              rules: [{
                required: true,
                validator: validatePhone,
                //message: "请输入联系方式",
                trigger: "blur"
              }],
            },
            {
              label: "户籍地",
              prop: "householdAddr",
              //type: "select",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入户籍地",
                trigger: "blur"
              }],
            },
            {
              label: "户籍详址",
              prop: "householdAddrDetail",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入户籍详址",
                trigger: "blur"
              }],
            },
            {
              label: "现住地",
              prop: "liveAddr",
              //type: "select",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入现住地",
                trigger: "blur"
              }],
            },
            {
              label: "现住详址",
              prop: "liveAddrDetail",
              type: "input",
              hide: true,
              span: 8,
              rules: [{
                required: true,
                message: "请输入现住详址",
                trigger: "blur"
              }],
            },

            {
              label: "经度",
              prop: "lng",
              type: "input",
              hide: true,
              span: 8,
            },
            {
              label: "纬度",
              prop: "lat",
              type: "input",
              hide: true,
              span: 8,
            },
           {
              label: "人口类型",
              prop: "populationType",
              type: "select",
              search: true,
              span: 8,
              addDisplay: true,
              editDisplay: true,
              viewDisplay: false,
              rules: [{
                required: true,
                message: "请输入人口类型",
                trigger: "blur"
              }],
              dicData: [
                {
                  label: "户籍人口",
                  value: "01"
                },
                {
                  label: "流动人口",
                  value: "02"
                },
                {
                  label: "境外人口",
                  value: "03"
                }
              ],
            },
            ]
          },
        data: []
      };
    },

    components:{
      peopleDetail
    },

    watch: {
        'form.gridId'() {
            if (!validatenull(this.form.gridId)) {
                getHouseList(this.form.gridId).then(res => {
                  const column = this.findObject(this.option.group, "houseId");
                  column.dicData = res.data.data;
                });
            }
        },
      },

    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.allPopulation_add, false),
          viewBtn: this.vaildData(this.permission.allPopulation_view, false),
          delBtn: this.vaildData(this.permission.allPopulation_delete, false),
          editBtn: this.vaildData(this.permission.allPopulation_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      //人员详情弹窗
    handleView(id) {
        // var options = {
        //     title: '人员详情',
        //     width: "1000",
        //     url:  "gis/peopleDetail/" + id
        // };
        // $.modal.openOptions(options);
      this.peopleId = id
      this.componentKey += 1
      this.peopleDetailFlowBox = true
    },

      rowSave(row, done, loading) {
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          loading();
          window.console.log(error);
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (["add"].includes(type)) {
          setTimeout(() => {
            this.form.populationType = '1';
          }, 10);
        }
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data.data;
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage){
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize){
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
.el-form-item__content{
   margin-left: 130px ! important;
}
.el-form-item__label {
  width: 130px ! important;
}
</style>
